<!doctype html>
<#import "/spring.ftl" as spring />
<#assign security=JspTaglibs["/META-INF/security.tld"] />

<html lang="en">
<head>
<#include "include/css.ftl">
    <style>
        .myFrame{
            position: absolute;
            bottom: 0;
            scrolling:auto;
            width: 100%;
            height: 100%;
            top: 0;
        }
    </style>
</head>

<body class="adminbody">
<div id="main">
    <!-- top bar navigation -->
    <#include "include/header-bar.ftl">
    <!-- Left Sidebar -->
    <#include "include/left-menu.ftl">

    <div class="headerbarMsg row" style="display: none" id="disconnectMsg">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
            <div class="alert alert-danger" role="alert" style="text-align: center">
                <strong>与服务器的通讯已中断!</strong>
            </div>
        </div>
        </div>
    <iframe src="${ctx}/dashboard" class="myFrame" id="myFrame"> </iframe>
    <!-- END content-page -->

    <#include "include/footer.ftl">
</div>
<input type="hidden" id="websocketUrl" value="${websocketUrl}">
<!-- END main -->
<#include "include/js.ftl">

<script>
    $(document).ready(function () {
        // 点击左侧菜单跳转页面
        $("a.lm").on('click', function(e){
            var _url = $(this).attr("furl");
            console.log(_url);

            $("#sidebar-menu a.active").removeClass("active");
            $(this).addClass("active");

            // 登陆 超时检测，成功后再让iframe加载页面，否则跳转到登录页面
            var _csrf_token = $("meta[name='_csrf']").attr("content");
            var _csrf_header_name = $("meta[name='_csrf_header']").attr("content");
            $.ajax({
                type: "HEAD",
                beforeSend: function (request) {
                    request.setRequestHeader(_csrf_header_name, _csrf_token);
                },
                url: "${ctx}/logined",
                dataType: 'JSON',
                success: function (data, status, request) {
                    $("#myFrame").attr("src",_url);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    if(jqXHR.status == 440 ||jqXHR.status == 403 ||jqXHR.status == 401){
                        window.location.assign("${ctx}/");
                    }else if (jqXHR.status >= 500) {
                        toastr.error('暂停服务，请耐心等待服务恢复。', '系统通知')
                    } else {
                        toastr.error('error code: ' + jqXHR.status, 'Error')
                    }
                }
            });
        });
    });

    // 退出
    function logout() {
        $("#logout_form").submit();
    }

    // WebSocket
    $(function () {
        var wsServerAddr = $("#websocketUrl").val();//"ws://192.168.15.42:19830/TcpTester/myHandler";
        var ws = $.websocket(wsServerAddr, {
            open: function (e) {
                console.log("WebSocket open on "+wsServerAddr);
                ws.send("open-->Hello WebSockets!");
            },
            close: function (e) {
                console.log("WebSocket closed");
                $("#disconnectMsg").show();
            },
            message:function(e){
                console.log("client receive a message");
                console.log(e);
            },
            events: {
                logout: function (e) {
                    console.log(e.data);
                    toastr.warning(e.data.logoutReason);
                }

            }
        });
    });

</script>
<!-- END Java Script for this page -->

</body>
</html>